package com.base.api.service.phoneBill.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.api.dto.phoneBill.PhoneBillListDTO;
import com.base.api.service.phoneBill.PhoneBillApiService;
import com.base.result.PageUtils;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.entity.PhoneBill;
import com.base.self.mapper.PhoneBillMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class PhoneBillApiServiceImpl implements PhoneBillApiService {
    private final PhoneBillMapper phoneBillMapper;

    public PhoneBillApiServiceImpl(PhoneBillMapper phoneBillMapper) {
        this.phoneBillMapper = phoneBillMapper;
    }

    @Override
    @Transactional(readOnly = true)
    public ResponseResult phoneBillList(PhoneBillListDTO phoneBillListDTO) {
        final Map<String, Object> data = PageUtils.getDateMap(() ->
                        phoneBillMapper.selectPage(
                                PageUtils.getPage(
                                        new Page<>(),
                                        phoneBillListDTO.getOffset(),
                                        phoneBillListDTO.getLimit()
                                ),
                                new LambdaQueryWrapper<PhoneBill>()
                                        .eq(PhoneBill::getUserId, phoneBillListDTO.getUserId())
                                        .eq(PhoneBill::getIsDel, 0)
                                        .orderByDesc(PhoneBill::getCreateTime)
                        )
                );
        return ResponseResultUtils.getResponseResultS("查询成功", data);
    }
}
